//
//  WarnDetailDescribeView.m
//  Rail
//
//  Created by hychen on 2018/2/28.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "WarnDetailDescribeView.h"

@interface WarnDetailDescribeView ()
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;

@property (weak, nonatomic) IBOutlet UIImageView *leftImage;
@property (weak, nonatomic) IBOutlet UIImageView *midImage;
@property (weak, nonatomic) IBOutlet UIImageView *rightImage;


@property (weak, nonatomic) IBOutlet UILabel *warnLevelLabel;
@property (weak, nonatomic) IBOutlet UIImageView *warnLevelImage;
@property (weak, nonatomic) IBOutlet UILabel *warnTypeLabel;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *videoViewHeightConstraints;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imagesViewHeightConstraints;
@end
@implementation WarnDetailDescribeView

+ (CGFloat)heightForDescribeView:(WarnDetailModel *)model {
    //标题
    CGFloat height = 61;
    //内容
    NSString *content = model.content?:@"";
    CGFloat contentH = [content sizeWithFont:[UIFont systemFontOfSize:14.f] maxW:SCREEN_WIDTH-30].height;
    height += contentH;
    if (contentH > 0) {
        height += 10;
    }
    //视频
    if ([model.video_url isKindOfClass:[NSString class]] && model.video_url.length > 0) {
        height += 180;
    }
    //图片
    if (model.snapshot_url1.length > 0 || model.snapshot_url2.length > 0 || model.snapshot_url3.length > 0) {
        height += 130;
    }
    //报警类型栏
    height += 50;
    //分割线
    height += 10;
    return height;
}

+ (instancetype)describeView {
    return [[[NSBundle mainBundle] loadNibNamed:@"WarnDetailDescribeView" owner:self options:nil] lastObject];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.clipsToBounds = YES;
    [self.leftImage addTapGestureWithTarget:self action:@selector(imageClick:)];
    [self.midImage addTapGestureWithTarget:self action:@selector(imageClick:)];
    [self.rightImage addTapGestureWithTarget:self action:@selector(imageClick:)];
    BMLineView *lineView = [BMLineView lineViewWithFrame:CGRectZero];
    [self addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.mas_equalTo(10);
    }];
}

- (void)setModel:(WarnDetailModel *)model {
    _model = model;
    self.titleLabel.text = model.event_name;
    self.contentLabel.text = model.content;
    if (model.video_url.length > 0) {
        self.videoViewHeightConstraints.constant = 180;
    }else {
        self.videoViewHeightConstraints.constant = 0;
    }
    
    if (model.snapshot_url1.length > 0 || model.snapshot_url2.length > 0 || model.snapshot_url3.length > 0) {
        self.imagesViewHeightConstraints.constant = 130;
        
        if (model.snapshot_url1.length > 0) {
            [self.leftImage sd_setImageWithURL:[NSURL URLWithString:model.snapshot_url1] placeholderImage:ImageNamed_bb(@"home_list_default_img")];
            if (model.snapshot_url2.length > 0) {
                [self.midImage sd_setImageWithURL:[NSURL URLWithString:model.snapshot_url2] placeholderImage:ImageNamed_bb(@"home_list_default_img")];
                if (model.snapshot_url3.length > 0) {
                    [self.rightImage sd_setImageWithURL:[NSURL URLWithString:model.snapshot_url3] placeholderImage:ImageNamed_bb(@"home_list_default_img")];
                }
                else {
                    self.rightImage.hidden = YES;
                }
            }
            else {
                self.rightImage.hidden = YES;
                if (model.snapshot_url3.length > 0) {
                    [self.midImage sd_setImageWithURL:[NSURL URLWithString:model.snapshot_url3] placeholderImage:ImageNamed_bb(@"home_list_default_img")];
                }
                else {
                    self.midImage.hidden = YES;
                }
            }
        }
        else {
            //最多显示两张
            self.rightImage.hidden = YES;
            if (model.snapshot_url2.length > 0) {
                [self.leftImage sd_setImageWithURL:[NSURL URLWithString:model.snapshot_url2] placeholderImage:ImageNamed_bb(@"home_list_default_img")];
                if (model.snapshot_url3.length > 0) {
                    [self.midImage sd_setImageWithURL:[NSURL URLWithString:model.snapshot_url3] placeholderImage:ImageNamed_bb(@"home_list_default_img")];
                }
                else {
                    self.midImage.hidden = YES;
                }
    
            }
            else {
                //最多显示一张
                self.midImage.hidden = YES;
                if (model.snapshot_url3.length > 0) {
                    [self.leftImage sd_setImageWithURL:[NSURL URLWithString:model.snapshot_url3] placeholderImage:ImageNamed_bb(@"home_list_default_img")];
                }
                else {
                    //一张都不显示
                    self.leftImage.hidden = YES;
                }
            }
        }

    }else {
        self.imagesViewHeightConstraints.constant = 0;
    }
    
    [self setWarnType:model.event_level];
    
    self.warnTypeLabel.text = [WarnLevelConfig alertTypeStringWithType:model.alert_type];
}

- (void)imageClick:(UIGestureRecognizer *)gest {
    if (self.BlockClickImage) {
        self.BlockClickImage(gest.view.tag, self.model);
    }
}

- (IBAction)playBtnClick:(UIButton *)sender {
    if (self.BlockCliclPlayBtn) {
        self.BlockCliclPlayBtn(self.playButton);
    }
}

- (void)setWarnType:(WarnLevelType)type {
    WarnLevelInfoModel *model = [WarnLevelConfig warnLevelInfoWithType:type];
    if (![model isKindOfClass:[WarnLevelInfoModel class]]) {
        self.warnLevelLabel.text = @"";
        self.warnLevelImage.image = nil;
        return;
    }
    self.warnLevelLabel.text = [NSString stringWithFormat:@"%@%@",model.title,@"警报"];
    self.warnLevelImage.image = model.image;
    self.warnLevelLabel.textColor = model.color;
}
@end
